package cn.tomoki.myapplication;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import cn.tomoki.adapters.ArticleGridAdapter;
import cn.tomoki.beans.ItemBeanArticle;
import cn.tomoki.config.URL;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

//查询预约界面
public class AppointmentActivity extends AppCompatActivity {

    private RecyclerView appointmentOut;
    private ArrayList<ItemBeanArticle> mDate;
    private ArticleGridAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.appointment);
        ImageButton myBreak =findViewById(R.id.breakButton);
        TextView appointHistory = findViewById(R.id.appointHistory);
        appointHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AppointmentActivity.this,AppointHistoryActivity.class);
                startActivity(intent);
            }
        });
        myBreak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //找到组件
        appointmentOut = this.findViewById(R.id.appointmentRecyclerView);

        //设置样式
//        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        appointmentOut.setLayoutManager(layoutManager);
        //准备模拟数据
        initDate();
    }

    private void initListener() {
        adapter.setOnItemClickListener(new ArticleGridAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //这里处理点击事件，该干嘛就干嘛
                ItemBeanArticle itemBeanArticle = mDate.get(position);
                Intent intent = new Intent(AppointmentActivity.this, ProductDetailsActivity.class);
                intent.putExtra("id",itemBeanArticle.id);
                startActivity(intent);
            }
        });
    }
    private void initDate() {
        //创建数据集合
        mDate = new ArrayList<>();
        //接收后端数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String url = (URL.url + "/app/getProductAll");
                    //创造HTTP客户端
                    OkHttpClient client = new OkHttpClient()
                            .newBuilder()
                            .connectTimeout(200, TimeUnit.SECONDS)
                            .readTimeout(250, TimeUnit.SECONDS)
                            .build();
                    //创造http请求
                    Request request = new Request.Builder()
                            .url(url)
                            .build();
                    //执行发送的指令
                    Response response = client.newCall(request).execute();
                    String responseDate = response.body().string();
                    JSONArray jsonArray = new JSONArray(responseDate);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        //创建数据对象
                        ItemBeanArticle date = new ItemBeanArticle();
                        date.articleImage = R.drawable.wire_disc;
                        date.id = jsonObject.getString("id");
                        date.name = jsonObject.getString("name");
                        date.number = "可预订"+jsonObject.getString("usable")+"件";
                        mDate.add(date);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //创建适配器
                            adapter = new ArticleGridAdapter(mDate);
                            //设置到view里面
                            appointmentOut.setAdapter(adapter);
                            //初始化事件
                            initListener();
                        }
                    });
                }catch (Exception e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AppointmentActivity.this,"连接不上网络啦(。﹏。)",Toast.LENGTH_SHORT);
                        }
                    });
                }
            }
        }).start();
    }


}